package com.fbty.mt.action.user;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.fbty.base.action.BaseAction;
import com.fbty.base.domain.Page;
import com.fbty.mt.domain.CheckInfo;
import com.fbty.mt.domain.OnlyIpInfo;
import com.fbty.mt.domain.UserIpInfo;
import com.fbty.mt.service.ProxyService;
import com.fbty.mt.service.UserIpInfoService;
import com.fbty.mt.service.impl.IpServiceImpl;

public class ChangeAction extends BaseAction {
	private static final long serialVersionUID = 1L;
	
	
	private Integer id;
	
	private Integer ip;
	
	private String type="";
	
	private String country="";
	
	private String region="";
	
	private String city="";
	
	private String item;
	
	private Integer change;
	
	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getCountry() {
		return country;
	}

	public void setCountry(String country) {
		this.country = country;
	}

	public String getRegion() {
		return region;
	}

	public void setRegion(String region) {
		this.region = region;
	}

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public String getItem() {
		return item;
	}

	public void setItem(String item) {
		this.item = item;
	}

	public String getParam() {
		return param;
	}

	public void setParam(String param) {
		this.param = param;
	}

	public int getPageNo() {
		return pageNo;
	}

	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public List<String> getIpItems() {
		return ipItems;
	}

	public void setIpItems(List<String> ipItems) {
		this.ipItems = ipItems;
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	public boolean isFlag() {
		return flag;
	}

	public void setFlag(boolean flag) {
		this.flag = flag;
	}

	public Map getParms() {
		return parms;
	}

	public void setParms(Map parms) {
		this.parms = parms;
	}

	public Integer getId() {
		return id;
	}

	public Integer getIp() {
		return ip;
	}

	public UserIpInfoService getUserIpInfoService() {
		return userIpInfoService;
	}

	public IpServiceImpl getIpInfoService() {
		return ipInfoService;
	}

	public ProxyService getProxyService() {
		return proxyService;
	}

	public void setState(int state) {
		this.state = state;
	}
	private String param;
	
	private int pageNo=1;
	
	private int pageSize=10;
	
	private List<String> ipItems;
	
	private Page page;
	
	private boolean flag;
	
	@SuppressWarnings("rawtypes")
	private Map parms=new HashMap();
	
	private UserIpInfoService userIpInfoService;
	
	private IpServiceImpl ipInfoService;
	
	private ProxyService proxyService;
	private int state;//1成功 0 表示更换失败 2正常使用 3监测状态不够4已经到期
	public String execute(){
		try {
			UserIpInfo uif = userIpInfoService.findUserIpById(id);
			if(uif.getEndTime()<System.currentTimeMillis()){
				state=4;
				return "success";
			}
			if(uif.getNum()<3){
				state=3;//监测少于3次
			}else{
				OnlyIpInfo only = ipInfoService.findOnlyById(ip);
				CheckInfo checkInfo = proxyService.checkConnection(only.getEntranceIp(), only.getEntrancePort(), uif.getUsername(), uif.getPassword());
				if(checkInfo.getReslut()==0){
					state=2;//能够正常使用
				}else{
					state = 1;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return "success";
	}
	
	public String autoChange(){
		try {
			UserIpInfo uif = userIpInfoService.findUserIpById(id);
			if(uif.getEndTime()<System.currentTimeMillis()){
				state=4;
				return "fail";
			}
			if(uif.getNum()<3){
				state=3;//监测少于3次
			}else{
				
					
				parms.put("region", "%"+region+"%");
				parms.put("type","".equals(type)?"%"+type+"%":type);
				parms.put("country", "%"+country+"%");
				parms.put("city", "%"+city+"%");
				parms.put("used", 1);
				page = ipInfoService.pageOnlyIpInfos(pageNo, pageSize, flag, parms);
					
				return "autoChange";
				
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return "fail";
	}
	/**
	 * 自动更换最后一步
	 * @return
	 * @throws Exception
	 */
	public String change() throws Exception{
		
		state=0;
		//找到需要更换的纪录
		UserIpInfo uif = userIpInfoService.findUserIpById(id);
		//找到新IP信息
		OnlyIpInfo only = ipInfoService.findOnlyById(change);
		if(uif.getEndTime()<System.currentTimeMillis()){
			state=4;
		}if(uif.getNum()<3){
			state=3;//监测少于3次
		}else{
			state=userIpInfoService.modifyOnlyIP(uif,only);
		}
		
		return "success";
	}
	
	public void setId(Integer id) {
		this.id = id;
	}

	public void setIp(Integer ip) {
		this.ip = ip;
	}

	public void setUserIpInfoService(UserIpInfoService userIpInfoService) {
		this.userIpInfoService = userIpInfoService;
	}

	public void setIpInfoService(IpServiceImpl ipInfoService) {
		this.ipInfoService = ipInfoService;
	}
	public int getState() {
		return state;
	}
	public void setProxyService(ProxyService proxyService) {
		this.proxyService = proxyService;
	}
	
	public void setChange(Integer change) {
		this.change = change;
	}
	
	public Integer getChange() {
		return change;
	}
	
}
